package bigo.todolist.avocado;

import android.app.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/** 
 * Set up the login page
 * @authorAlex Irlik, Steve Fox, TiOluwa Olarewaju, Derek Miller
 * Big-Ovacado Task Force
 * Version 1.0
 */
public class Login extends Activity implements View.OnClickListener{

        Button login;
        Button register;
        EditText username;
        EditText password;
        
        /**
         * Initializations for when page loads
         */
        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.login); //Set the layout of the window
                
                idSet(); //Call method to set the ids for each variable
                
                login.setOnClickListener(this); //Create click listener for login button
                register.setOnClickListener(this); //Create click listener for register button
        }

        /**
         * Instantiate UI variables
         */
        private void idSet() {
                login = (Button) findViewById(R.id.login); //Set id
                register = (Button) findViewById(R.id.register); //Set id
                username = (EditText) findViewById(R.id.usernameEdit); //Set id
                password = (EditText) findViewById(R.id.passEdit); //Set id
        }
        
        /**
         * Checks if the text boxes are blank
         * @param userText
         * @param passwordText
         * @return boolean
         */
        public boolean checkCredentialsLength(String userText, String passwordText) {
        	if(username.length() > 0 && password.length() >0)
        		return true;
        	else {
        		return false;
        	}
        }
        
        
        /**
         * Checks if given username and password exists in the SQL database
         * @param userText
         * @param passwordText
         * @return boolean
         */
        public boolean loginProcedure(String userText, String passwordText) {
                        DBUserAdapter dbUser = new DBUserAdapter(Login.this);
                        dbUser.open();
                        if(dbUser.Login(userText, passwordText))
                        {
                                dbUser.close();
                                return true;
                        }else{
                                dbUser.close();
                                return false;
                        }
        }
        
        /**
         * Actions to perform when given view is clicked
         * 
         * @param view - the view that was clicked upon
         */
        public void onClick(View view) {
                switch(view.getId())
                {
                        case R.id.login: //What happens when login is clicked
                                Editable user = username.getText(); 
                                Editable passwd = password.getText();
                                String userText = user.toString(); //Gets the username from the EditText
                                String passwordText = passwd.toString(); //Gets the password from EditText
                        
                                try{
                                        if (checkCredentialsLength(userText, passwordText) == true && loginProcedure(userText, passwordText) == true) {
                                                Toast.makeText(Login.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
                                                //Class ourClass = Class.forName("bigo.todolist.avocado.TaskList");
                                                Intent ourIntent = new Intent(Login.this, TaskList.class);
                                                Bundle sendUsername = new Bundle(); //Prepare and send a bundle containing username
                                                sendUsername.putString("username", userText);
                                                ourIntent.putExtras(sendUsername);
                                                startActivity(ourIntent);
                                        } else {
                                                Toast.makeText(Login.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
                                        }
                                }catch(Exception e)
                                {
                                        Toast.makeText(Login.this,e.getMessage(), Toast.LENGTH_LONG).show();
                                }
                                break;
                        case R.id.register: //What happens when register is click
                                
                                try{
                                        Class ourClass = Class.forName("bigo.todolist.avocado.Register");
                                        Intent ourIntent = new Intent(Login.this, ourClass);
                                        startActivity(ourIntent);

                                        break;
                                }catch(Exception e)
                                {
                                        e.printStackTrace();
                                }
                                
                                
                }
        
        }
}